//
//  vdDigit.m
//  Void Dead
//
//  Created by Sidney Just on 14.12.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdDigit.h"


@implementation vdDigit

- (void)update {
	if(mode == 1)
	{
		if(iDigit)
		{
			[self setString:[NSString stringWithFormat:@"%@%i", preString, *iDigit]];
			iOld = *iDigit;
		}
	}
	
	if(mode == 2)
	{
		if(fDigit)
		{
			[self setString:[NSString stringWithFormat:@"%@%f", preString, *fDigit]];
			fOld = *fDigit;
		}
	}
}

- (void)draw:(NSTimeInterval)deltaTime {
	if(mode == 0)
		return;
	
	if(mode == 1)
	{
		if(iDigit)
		{
			if(*iDigit != iOld)
			{
				[self setString:[NSString stringWithFormat:@"%@%i", preString, *iDigit]];
				iOld = *iDigit;
			}
		}
	}
	
	if(mode == 2)
	{
		if(fDigit)
		{
			if(*fDigit != fOld)
			{
				[self setString:[NSString stringWithFormat:@"%@%f", preString, *fDigit]];
				 fOld = *fDigit;
			}
		}
	}
	
	[super draw:deltaTime];
}

#pragma mark -
#pragma mark Variables

- (void)reset {
	iDigit = NULL;
	fDigit = NULL;
}

- (void)setPreString:(NSString *)str {
	if(str)
	{
		[preString release];
		preString = [[NSString alloc] initWithString:str];
	} else {
		[preString release];
		preString = [[NSString alloc] initWithString:@""];
	}
	[self update];
}

- (void)setIntValue:(int *)val {
	[self reset];
	iDigit = val;
	iOld = *iDigit + 1;
	mode = 1;
}

- (void)setFloatValue:(float *)val {
	[self reset];
	fDigit = val;
	fOld = *fDigit + 1;
	mode = 2;
}

#pragma mark -
#pragma mark Con/Destructor

+ (vdDigit *)digitWithInt:(int *)val {
	vdDigit *digit = [[vdDigit alloc] init];
	[digit setIntValue:val];
	return digit;
}

+ (vdDigit *)digitWithFloat:(float *)val {
	vdDigit *digit = [[vdDigit alloc] init];
	[digit setFloatValue:val];
	return digit;
}

- (id)init {
	if(self = [super init])
	{
		[self reset];
		mode = 0;
		preString = [[NSString alloc] initWithString:@""];
	}
	return self;
}

- (void)dealloc {
	[preString release];
	
	[super dealloc];
}

@end
